﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace E_ICS.E_ICS
{
    public partial class frmMAWBsearch : Form
    {
        Main.frmMain2 main;
        DataSet da = new DataSet();
        string MAWB,Prefix,Serial, HAWB, flt_no, flt_date,dest, Seq, AgentCode, AgentName,NatureOfGood,shipment_type;
        string Selhawb,SelFlt_no, SelFlt_date, Seldest, SelSeq, SelAgentCode, SelAgentName, SelNatureOfGood, Selshipment_type;
        public frmMAWBsearch(DataSet dataset, Main.frmMain2 _main)
        {
            InitializeComponent();
            da = dataset;
            main = _main;
        }

        private void btnMAWBAdd_Click(object sender, EventArgs e)
        {
            E_ICS.frmAddMAWB am1 = new E_ICS.frmAddMAWB(main);
            if (am1.ShowDialog() == DialogResult.OK)
            {
                this.DialogResult = DialogResult.OK;
                ClearValueToAddMAWB();
                this.Close();
            }
           

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ClearValueToAddMAWB();

            this.Dispose();
        }

        private void frmMAWBsearch_Load(object sender, EventArgs e)
        {
           DataTable MAWB_TB = CreateMAWB_TB(da);
           //dataGridView1.DataSource = da.Tables[0];

           dataGridView1.DataSource = MAWB_TB;
           dataGridView1.Columns[6].Visible = false;
           dataGridView1.Columns[7].Visible = false;
           dataGridView1.Columns[8].Visible = false;
           dataGridView1.Columns[9].Visible = false;
           //dataGridView1.Rows[dataGridView1.Rows.Count - 2].Cells[0].Selected = false;
           //dataGridView1.Rows[dataGridView1.Rows.Count-2].Selected = true;
           dataGridView1.Rows[0].Cells[0].Selected = false;
           Selectdata();
           btnSelect.Focus();
          

        }
        private DataTable CreateMAWB_TB(DataSet ds)
        {
            DataTable TW = new DataTable();
            ///////////create head column/////////////////
            TW.Columns.Add("MAWB", typeof(string));
            //TW.Columns.Add("Prefix", typeof(string));
            //TW.Columns.Add("Serial", typeof(string));
            TW.Columns.Add("HAWB", typeof(string));
            TW.Columns.Add("Flight No", typeof(string));
            TW.Columns.Add("Flight Date", typeof(string));
            TW.Columns.Add("Destination", typeof(string));
            TW.Columns.Add("Seq", typeof(string));
            TW.Columns.Add("Agent Code",typeof(string));
            TW.Columns.Add("Agent Name", typeof(string));
            TW.Columns.Add("Nature Of Good", typeof(string));
            TW.Columns.Add("Shipment Type", typeof(string));
            ///////////////////////////////////////////////
            int row = ds.Tables[0].Rows.Count;
            for (int i = 0; i < row; i++)
            {
                MAWB = ds.Tables[0].Rows[i]["mawb_prefix"].ToString().Trim() +"-"+ ds.Tables[0].Rows[i]["mawb_sn"].ToString().Trim();
                //Prefix = ds.Tables[0].Rows[i]["mawb_prefix"].ToString().Trim();
                //Serial = ds.Tables[0].Rows[i]["mawb_sn"].ToString().Trim();
                HAWB = ds.Tables[0].Rows[i]["hawb"].ToString().Trim();
                flt_no = ds.Tables[0].Rows[i]["display_flight_number"].ToString().Trim();
                flt_date = ds.Tables[0].Rows[i]["display_flight_date"].ToString().Trim();
                dest = ds.Tables[0].Rows[i]["destination"].ToString().Trim();
                Seq = ds.Tables[0].Rows[i]["partial_seq"].ToString().Trim();
                AgentCode = ds.Tables[0].Rows[i]["agent_code"].ToString().Trim();
                AgentName= ds.Tables[0].Rows[i]["agent_name"].ToString().Trim();
                NatureOfGood = ds.Tables[0].Rows[i]["nature_of_goods"].ToString().Trim();
                shipment_type = ds.Tables[0].Rows[i]["shipment_type"].ToString().Trim();
                TW.Rows.Add(MAWB, HAWB, flt_no, flt_date, dest, Seq, AgentCode, AgentName, NatureOfGood, shipment_type);
            }
            return TW;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            try
            {
                if (index ==-1)
                {
                     InserttoAddMAWB(dataGridView1.Rows.Count-2);
                     InserttoarrayMain2(dataGridView1.Rows.Count - 2);
                }
                string[] array = new string[9] { Selhawb, SelFlt_no, SelFlt_date, Seldest, SelSeq, SelAgentCode, SelAgentName, SelNatureOfGood, Selshipment_type };
                main.getDataFromMAWBSearch(array);
                this.DialogResult = DialogResult.OK;
                this.Dispose();
            }
            catch 
            {
                
                //throw;
            }
           
        }
        private void Selectdata()
        {
            //int index = dataGridView1.Rows.Count - 2;
            if (dataGridView1.Rows[index].Cells[0].Value != null)
            {
                Selhawb = dataGridView1.Rows[index].Cells[1].Value.ToString();
                SelFlt_no = dataGridView1.Rows[index].Cells[2].Value.ToString();
                SelFlt_date = dataGridView1.Rows[index].Cells[3].Value.ToString();
                Seldest = dataGridView1.Rows[index].Cells[4].Value.ToString();
                //SelSeq = dataGridView1.Rows[index].Cells[5].Value.ToString();
                SelSeq = dataGridView1.Rows.Count.ToString();
                SelAgentCode = dataGridView1.Rows[index].Cells[6].Value.ToString();
                SelAgentName = dataGridView1.Rows[index].Cells[7].Value.ToString();
                SelNatureOfGood = dataGridView1.Rows[index].Cells[8].Value.ToString();
                Selshipment_type = dataGridView1.Rows[index].Cells[9].Value.ToString();
                InserttoarrayMain2(index);

                InserttoAddMAWB(index);


            }
        }
        int index;
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                index = e.RowIndex;
                if (e.RowIndex >= 0)
                {
                    //dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                    dataGridView1.Rows[e.RowIndex].Selected = true;
                    if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
                    {
                        Selhawb = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                        SelFlt_no = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
                        SelFlt_date = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
                        Seldest = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
                        //SelSeq = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
                        SelSeq = dataGridView1.Rows.Count.ToString();
                        SelAgentCode = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
                        SelAgentName = dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString();
                        SelNatureOfGood = dataGridView1.Rows[e.RowIndex].Cells[8].Value.ToString();
                        Selshipment_type = dataGridView1.Rows[e.RowIndex].Cells[9].Value.ToString();
                        InserttoarrayMain2(e.RowIndex);

                        InserttoAddMAWB(e.RowIndex);


                    }
                }
            }
            catch (Exception)
            {
                
                //
            }
        }

        private void InserttoarrayMain2(int p)
        {
            Selhawb = dataGridView1.Rows[p].Cells[1].Value.ToString();
            SelFlt_no = dataGridView1.Rows[p].Cells[2].Value.ToString();
            SelFlt_date = dataGridView1.Rows[p].Cells[3].Value.ToString();
            Seldest = dataGridView1.Rows[p].Cells[4].Value.ToString();
            //SelSeq = dataGridView1.Rows[p].Cells[5].Value.ToString();
            SelSeq = dataGridView1.Rows.Count.ToString();
            SelAgentCode = dataGridView1.Rows[p].Cells[6].Value.ToString();
            SelAgentName = dataGridView1.Rows[p].Cells[7].Value.ToString();
            SelNatureOfGood = dataGridView1.Rows[p].Cells[8].Value.ToString();
            Selshipment_type = dataGridView1.Rows[p].Cells[9].Value.ToString();
           // throw new NotImplementedException();
        }
        
        private void InserttoAddMAWB(int p)
        {
           // ValueTofrmAddMAWB inserttoaddmawb = new ValueTofrmAddMAWB();
            
            ValueTofrmAddMAWB.SelPrefix = da.Tables[0].Rows[p]["mawb_prefix"].ToString().Trim();
            ValueTofrmAddMAWB.SelSerial = da.Tables[0].Rows[p]["mawb_sn"].ToString().Trim();
            ValueTofrmAddMAWB.Selhawb = dataGridView1.Rows[p].Cells[1].Value.ToString();
          //  ValueTofrmAddMAWB.SelFlt_no = dataGridView1.Rows[p].Cells[2].Value.ToString();
            //ValueTofrmAddMAWB.SelFlt_no = da.Tables[0].Rows[p]["flight_number"].ToString().Trim();
            //ValueTofrmAddMAWB.SelFlt_date = dataGridView1.Rows[p].Cells[3].Value.ToString();
            //ValueTofrmAddMAWB.Seldest = dataGridView1.Rows[p].Cells[4].Value.ToString();
            //ValueTofrmAddMAWB.SelSeq = dataGridView1.Rows[p].Cells[5].Value.ToString();
            //ValueTofrmAddMAWB.SelAgentCode = dataGridView1.Rows[p].Cells[6].Value.ToString();
            //ValueTofrmAddMAWB.SelAgentName = dataGridView1.Rows[p].Cells[7].Value.ToString();
            //ValueTofrmAddMAWB.SelNatureOfGood = dataGridView1.Rows[p].Cells[8].Value.ToString();
            //ValueTofrmAddMAWB.Selshipment_type = dataGridView1.Rows[p].Cells[9].Value.ToString();
            //ValueTofrmAddMAWB.Selpiece_count = da.Tables[0].Rows[p]["piece_count"].ToString().Trim();
            //ValueTofrmAddMAWB.Selcarrier = da.Tables[0].Rows[p]["carrier"].ToString().Trim();
            //ValueTofrmAddMAWB.Selflight_date = da.Tables[0].Rows[p]["flight_date_std"].ToString().Trim();
        }
        public void ClearValueToAddMAWB()
        {
            ValueTofrmAddMAWB.SelPrefix = "";
            ValueTofrmAddMAWB.SelSerial = "";
            ValueTofrmAddMAWB.Selhawb = "";
            ValueTofrmAddMAWB.SelFlt_no = "";
            ValueTofrmAddMAWB.SelFlt_date = "";
            ValueTofrmAddMAWB.Seldest = "";
            ValueTofrmAddMAWB.SelSeq = "";
            ValueTofrmAddMAWB.SelAgentCode = "";
            ValueTofrmAddMAWB.SelAgentName = "";
            ValueTofrmAddMAWB.SelNatureOfGood = "";
            ValueTofrmAddMAWB.Selshipment_type = "";
            ValueTofrmAddMAWB.Selpiece_count = "";
            ValueTofrmAddMAWB.Selcarrier = "";
            ValueTofrmAddMAWB.Selflight_date = "";

        }
        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            ClearValueSelectMAWB();
            ClearValueToAddMAWB();
        }

        private void ClearValueSelectMAWB()
        {
            dataGridView1.ClearSelection();
            SelFlt_no = "";
            SelFlt_date = "";
            Seldest = "";
            SelSeq = "";
            SelAgentCode = "";
            SelAgentName = "";
            SelNatureOfGood = "";
            Selshipment_type = "";

        }   
    }
}
